package com.intramirror.wd.service.controller;

import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.intramirror.common.base.model.ApiResponse;
import com.intramirror.wd.business.manage.CouponStrategyManage;
import com.intramirror.wd.business.util.ChucResponse;
import com.intramirror.wd.model.enums.CouponGettingTypeEnum;
import com.intramirror.wd.model.enums.CouponPreConditionEnum;
import com.intramirror.wd.model.vo.coupon.S2cCouponStrategyVo;
import com.intramirror.wd.model.vo.page.PageVo;

@RestController
@RequestMapping(value = "/couponStrategy")
public class CouponStrategyController {

    @Autowired
    private CouponStrategyManage couponStrategyManage;

    @PostMapping(value = "")
    public Object save(@RequestBody S2cCouponStrategyVo strategy) throws Exception{
        return couponStrategyManage.saveCouponStrategy(strategy);
    }

    @PutMapping(value = "/{id}")
    public Object update(@PathVariable("id") Long id, @RequestBody S2cCouponStrategyVo strategy) throws Exception{
        if(id == null) {
            return new ApiResponse<>("id is null");
        }
        return couponStrategyManage.updateCouponStrategy(strategy, id);
    }

    @DeleteMapping(value = "/{id}")
    public Object del(@PathVariable("id") Long id) throws Exception{
        return couponStrategyManage.delCouponStrategy(Long.valueOf(id));
    }

    @GetMapping(value = "/{id}")
    public Object select(@PathVariable("id") Long id) throws Exception{
        S2cCouponStrategyVo couponStrategy = couponStrategyManage.getStrategySetailById(Long.valueOf(id));
        return couponStrategy == null ? new ChucResponse<>("null data") : couponStrategy;
    }

    @GetMapping(value = "")
    public Object getAllStrategy(HttpServletRequest request) throws Exception{
        PageVo<S2cCouponStrategyVo> allStrategy;
        String page = request.getParameter("page");
        String pageSize = request.getParameter("pageSize");
        if(StringUtils.isBlank(page) && StringUtils.isBlank(pageSize)) {
            allStrategy = couponStrategyManage.getAllStrategy(new S2cCouponStrategyVo(null, null));
        } else {
            allStrategy = couponStrategyManage.getAllStrategy(new S2cCouponStrategyVo(Integer.valueOf(page), Integer.valueOf(pageSize)));
        }
        return allStrategy == null ? new ChucResponse<>("null data") : allStrategy;
    }

    @GetMapping(value = "/getPreCondition/{isPublished}")
    public Object getPreCondition(@PathVariable("isPublished") Integer isPublished) throws Exception{
        if(isPublished == null) {
            return CouponPreConditionEnum.map;
        } else {
            return couponStrategyManage.getIsPublishedStrategy(isPublished);
        }
    }
    @GetMapping(value = "/getGettingType")
    public Object getGettingType() throws Exception{
        return CouponGettingTypeEnum.map;
    }

    @PutMapping(value = "/isPublished/{id}/{flag}")
    public Object isPublished(@PathVariable("id") Long id, @PathVariable("flag") Integer flag) throws Exception{
        if(id == null || flag == null) {
            return new ChucResponse<>("parameter is missing");
        }
        return couponStrategyManage.changeIsPublish(id, flag);
    }
}
